package leetcode.easy.getdecimalvalue;

import util.adt.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * <a href="https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/">题目链接</a>
 */
class Solution {
    public int getDecimalValue(ListNode head) {
        return mySolution(head);
    }

    private int mySolution(ListNode head) {
        List<Integer> data = new ArrayList<>();
        while (head != null) {
            data.add(head.val);
            head = head.next;
        }

        int ans = 0, SIZE = data.size();
        for (int i = 0; i < SIZE; i++) {
            if (data.get(i) == 1) {
                ans += Math.pow(2, SIZE - 1 - i);
            }
        }
        return ans;
    }

    private int official(ListNode head) {
        ListNode curNode = head;
        int ans = 0;
        while (curNode != null) {
            ans = ans * 2 + curNode.val;
            curNode = curNode.next;
        }
        return ans;
    }
}
